package cn.appsys.controller.developer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.devuser.DevUserService;


@Controller
@RequestMapping(value = "/dev")
public class DevLoginController {
   public final static String DEV_USER_SESSION="devUserSession";
	Logger logger = Logger.getLogger(DevLoginController.class);

	@Resource
	private DevUserService devUserService;

	@RequestMapping("/login")
	public String login() {
		logger.debug("===========登录=========");
		return "devlogin";
	}

	@RequestMapping("/dologin")
	public String dologin(@RequestParam String devCode, @RequestParam  String devPassword, HttpServletRequest request,
			HttpSession session) {
			System.out.println(devCode+"_____________________________");
		DevUser user = devUserService.getLoginUser(devCode, devPassword);
		if (null != user) {// 登录成功
			// 放入session
			session.setAttribute(DevLoginController.DEV_USER_SESSION, user);
			// 页面跳转（main.jsp）
			return "redirect:/dev/flatform/main";
		} else {
			// 页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			return "devlogin";
		}
	}

	@RequestMapping("/flatform/main")
	public String main(HttpSession session) {
		if (session.getAttribute(DevLoginController.DEV_USER_SESSION) == null)
			return "redirect:/dev/login";
		return "developer/main";
	}

	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		// 清除session
		session.removeAttribute(DevLoginController.DEV_USER_SESSION);
		return "devlogin";
	}

}
